import { concatEndTime, concatStartTime, shortcuts } from '@/plugins/date'
import { IFormSchemas, ITableColumns } from '@/type/table'
import { h } from 'vue'
import { ElLink } from 'element-plus'
import DictionaryShow from '@/components/dictionary/DictionaryShow.vue'
import { MATTER_TITLE_WIDTH } from '@/plugins/const'
import { EMatterStatus } from '@/components/statusShow/util'
import MatterShow from '@/components/statusShow/matter/MatterShow.vue'

export enum EEassignStr {
  sjjb = 1,
  qwjb,
  qzjb,
  qfjb,
}

export enum EIsFocus {
  是 = 1,
  否 = 0,
}

export const EAssignType = {
  [EEassignStr[1]]: '书记交办',
  [EEassignStr[2]]: '区委交办',
  [EEassignStr[3]]: '区长交办',
  [EEassignStr[4]]: '区府交办',
}

export const EAssignColor = {
  [EEassignStr[1]]: '#67C23A',
  [EEassignStr[2]]: '#409EFF',
  [EEassignStr[3]]: '#8D4EDA',
  [EEassignStr[4]]: '#E6A23C',
}

export enum EEventStatusColor {
  'primary' = 1,
  'success' = 3,
  'warning' = 2,
}

const eventStatusOptions = [
  {
    label: '正常推进',
    value: EMatterStatus['正常推进'],
  },
  {
    label: '已完成',
    value: EMatterStatus['已完成'],
  },
  {
    label: '已超时',
    value: EMatterStatus['已超时'],
  },
  {
    label: '未签收',
    value: EMatterStatus['未签收'],
  },
]

// 生成列表项
export function generateTableColumn(
  callback?: (columnType: string, row: Record<string, any>) => void,
) {
  const tableColumns: ITableColumns[] = [
    {
      label: 'ID',
      prop: 'id',
      width: 80,
    },
    {
      label: '事项标题',
      prop: 'matterTitle',
      minWidth: MATTER_TITLE_WIDTH,
      render: (row) => {
        return h(
          ElLink,
          {
            type: 'primary',
            underline: 'never',
            onClick: () => callback('matterTitle', row),
          },
          () => row.matterTitle,
        )
      },
    },
    {
      label: '交办类型',
      prop: 'assignType',
      minWidth: 100,
      render: (row) => {
        return h(DictionaryShow, {
          parentDictionaryValue: 'taskType',
          value: row.assignType,
        })
      },
    },
    {
      label: '业务领域',
      prop: '',
      minWidth: 120,
      render: (row) => {
        return h(DictionaryShow, {
          parentDictionaryValue: 'runway',
          value: row.domainId,
        })
      },
    },
    {
      label: '状态',
      prop: 'matterStatus',
      width: 100,
      render: (row) => {
        return h(MatterShow, {
          status: row.matterStatus,
        })
      },
    },
    {
      label: '是否重点',
      prop: 'isFocus',
      width: 100,
      render: (row) => {
        return h('span', EIsFocus[row.isFocus])
      },
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 220,
    },
  ]
  return tableColumns
}

// 处理请求参数
export function dealSearchParams(params: any) {
  return {
    ...params,
    startTime: concatStartTime(params?.time?.[0]),
    endTime: concatEndTime(params?.time?.[1]),
    matterStatus: params?.matterStatus ?? 0,
  }
}

const afterSearchSchame: IFormSchemas[] = [
  {
    label: '创建时间',
    prop: 'time',
    type: 'date-picker',
    span: 8,
    componentProps: {
      type: 'daterange',
      'range-separator': '到',
      'start-placeholder': '开始时间',
      'end-placeholder': '结束时间',
      'unlink-panels': true,
      editable: false,
      format: 'YYYY年MM月DD日',
      valueFormat: 'YYYY-MM-DD',
      shortcuts: shortcuts,
      clearable: true,
    },
  },
]

// 列表查询
export const searchSchame: IFormSchemas[] = [
  // {
  //   label: '交办类型',
  //   prop: 'taskType',
  //   type: (props) => {
  //     return h(DictionarySelect, {
  //       ...props,
  //       parentDictionaryValue: 'taskType',
  //       placeholder: '请选择交办类型',
  //     })
  //   },
  // },
  {
    label: '事项状态',
    prop: 'matterStatus',
    type: 'select',
    placeholder: '请选择事项状态',
    componentProps: {
      options: eventStatusOptions,
      clearable: true,
    },
  },
  ...afterSearchSchame,
]
